/**
 * Created by Administrator on 2017/3/21.
 */
import React, {Component} from 'react';
import {
  Form, Input, Radio, Select, Icon,
  Upload, Tooltip, DatePicker, Button,
} from 'antd';
import {connect} from 'dva';
import ReactDataGrid from 'react-data-grid';
import moment from 'moment'
import {download,renderReport} from '@/utils/utils';
import config from '@/config/config'
import find from 'lodash/find';

const FormItem = Form.Item;
const { TextArea } = Input;
const RadioGroup = Radio.Group;
const Option = Select.Option;

class AddPoliciesForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      users:[],
      parseData:[],
      columns:[]
    };
  }

  componentDidMount() {
    const that=this;
    let arr=[
      {name:'一:营业收入',key:'rysr',line:'1',offset:'0'},
      {name:'其中:主营业务收入',key:'zyywsr',line:'',offset:'2'},
      {name:'其它业务收入',key:'qtywsr',line:'',offset:'3'},
      {name:'减:营业成本',key:'rycb',line:'2',offset:'1'},
      {name:'其中:主营业务成本',key:'zyywcb',line:'',offset:'2'},
      {name:'其它业务成本',key:'qtywcb',line:'',offset:'3'},
      {name:'销售费用',key:'xsfy',line:'3',offset:'1'},
      {name:'营业税金及附加',key:'yysjjfj',line:'4',offset:'1'},
      {name:'管理费用',key:'glfy',line:'10',offset:'1'},
      {name:'其中:业务招待费',key:'ywzdf',line:'9',offset:'2'},
      {name:'财务费用',key:'cwfy',line:'11',offset:'1'},
      {name:'其中:利息支出',key:'lxzc',line:'12',offset:'2'},
      {name:'利息收入',key:'lxsr',line:'',offset:'3'},
      {name:'汇兑净损失',key:'hdjss',line:'13',offset:'3'},
      {name:'其它',key:'qt',line:'14',offset:'1'},
      {name:'加:投资收益',key:'tzsy',line:'15',offset:'1'},
      {name:'二:营业利润',key:'yylr',line:'',offset:'0'},
      {name:'加:营业外收入',key:'yywsr',line:'16',offset:'1'},
      {name:'其中:非流动资产处置利得',key:'fldzcczld',line:'',offset:'2'},
      {name:'政府补助(补贴收入)',key:'zfbz',line:'17',offset:'3'},
      {name:'减:营业外支出',key:'yywzc',line:'18',offset:'1'},
      {name:'其中:非流动资产处置损失',key:'fldzcczss',line:'',offset:'2'},
      {name:'三:利润总额',key:'lrze',line:'19',offset:'0'},
      {name:'减:所得税费用',key:'sdsfy',line:'',offset:'1'},
      {name:'四:净利润',key:'jlr',line:'20',offset:'0'},
    ]
    // let parseData=this.state.parseData
    // for(let i=0;i<arr.length;i++){
    //   parseData[i]={name:arr[i].name,key:arr[i].key}
    //   for(let j=0;j<this.state.originData.length;j++){
    //     parseData[i][`${this.state.originData[j].address}`]=this.state.originData[j][arr[i].key]
    //   }
    // }
    // console.log('parseData',parseData)
    // this.setState({parseData})

    let data=[]
    const items=JSON.parse(JSON.stringify(this.props.editRecord.items));
    const columns = [
      { key: "name", name: "",width:250,resizable: true,
        formatter: (event) => {
          return   <div  style={{
            textIndent:Number(event.row.offset)*30,
            fontWeight:event.row.offset==='0'?'bold':'',
          }}>
            {event.value}
          </div>;
        },},
    ];
    for(let i=0;i<items.length;i++){
      columns.push(  { key: items[i].name, name: items[i].name,resizable: true,
        formatter: (event) => {
          return   <div  style={{
          }}>
            {event.value.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
          </div>;
        }},)
    }
    this.setState({
      columns
    },function() {
      for(let i=0;i<arr.length;i++){
        data[i]={name:arr[i].name,key:arr[i].key,offset:arr[i].offset}
        for(let j=0;j<items.length;j++){
          let exit=find(items[j].items,function(o) {
            return o.key===arr[i].key
          })
          if(exit){
            data[i][`${items[j].name}`]=exit.value
          }
        }
      }

      console.log('data',data)
      this.setState({
        parseData:data
      })
    })
  }
  render() {
    const formItemLayoutWithLabel = {
      labelCol: {
        xs: {span: 24},
        sm: {span: 8},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 14},
      }
    };
    return (
      <div>
        <h1 style={{textAlign:'center'}}>利润表</h1>
        <Form onSubmit={this.handleSubmit} style={{textAlign:'center'}}>
          <FormItem
            style={{display:'inline-block',width:'33%'}}
            {...formItemLayoutWithLabel}
            label={(
              <span>
               采样日期
            </span>
            )}
          >
            <span style={{fontWeight:'bold'}}>{this.props.editRecord.customize.month}</span>
          </FormItem>
        </Form>
        <ReactDataGrid
          columns={this.state.columns}
          rowGetter={i => this.state.parseData[i]}
          rowsCount={this.state.parseData.length}
          minHeight={this.state.parseData.length*35+36}
        />
        <Form style={{marginTop:'10px'}} onSubmit={this.handleSubmit}>
          <FormItem
            style={{display:'inline-block',width:'33%'}}
            {...formItemLayoutWithLabel}
            label={(
              <span>
               部门负责人
            </span>
            )}
          >
            <span style={{fontWeight:'bold'}}>{this.props.editRecord.customize.director}</span>

          </FormItem>
          <FormItem
            style={{display:'inline-block',width:'33%'}}
            {...formItemLayoutWithLabel}
            label={(
              <span>
               制表
            </span>
            )}
          >
            <span style={{fontWeight:'bold'}}>{this.props.editRecord.customize.author}</span>

          </FormItem>
          {
            this.props.editRecord.original_files.length>0&&
            <FormItem
              style={{display:'inline-block',width:'33%'}}
              {...formItemLayoutWithLabel}
              label={'原文件'}
            >
              <div onClick={()=>{
                download(`${config.prefix}/reports/${this.props.editRecord.id}/original_files/${this.props.editRecord.original_files[0].key}/download??token=Bearer ${sessionStorage.getItem('token')}`)
              }} title={'点击下载'} style={{color:'blue',cursor:'pointer',textOverflow:'hidden',whiteSpace:'nowrap',overflow:'hidden'}}>
                {this.props.editRecord.original_files[0].name}
              </div>
            </FormItem>
          }
        </Form>
      </div>
    );
  }
}

const AddPoliciesFormWrap = Form.create()(AddPoliciesForm);
export default connect()(AddPoliciesFormWrap);


